#ifndef HTTP_CONN_H
#define HTTP_CONN_H

#include <sys/types.h>
#include <sys/uio.h>     // readv/writev
#include <arpa/inet.h>   // sockaddr_in
#include <stdlib.h>      // atoi()
#include <errno.h>      


#include "../Log/log.h"
#include "../POOL/sqlconnRAII.h"
#include "../Buffer/buffer.h"

#include "../MultiReactor/Socket.h"
#include "../MultiReactor/Acceptor.h"
#include "../MultiReactor/Channel.h"
#include "../MultiReactor/InetAddress.h"
#include "../MultiReactor/Callbacks.h"



#include "httprequest.h"
#include "httpresponse.h"

class EventLoop;

class HttpConn: noncopyable, public std::enable_shared_from_this<HttpConn> {
public:
    HttpConn(EventLoop *loop,                  
            int sockfd,
            const std::string &nameArg,
            InetAddress &localAddr,
            const InetAddress &peerAddr);
    ~HttpConn();
    

    EventLoop *getLoop() const { return loop_; }
    const std::string &name() const { return name_; }

    ssize_t read(int* saveErrno);
    ssize_t write(int* saveErrno);
    void OnProcess();
    void handleRead(Timestamp stamp);
    void handleWrite();
    void handleClose();
    void handleError();



    void connectDestroyed();

    void Close();
    
    int GetFd() const;

    int GetPort() const;
    
    const char* GetIP() const;

    sockaddr_in GetAddr() const;

    bool process();

    int ToWriteBytes() { 
        return iov_[0].iov_len + iov_[1].iov_len; 
    }

    bool IsKeepAlive() const {
        return request_.IsKeepAlive();
    }
    void connectEstablished();

    void setConnectionCallback(const ConnectionCallback &cb)
    { connectionCallback_ = cb; }
    void setMessageCallback(const MessageCallback &cb)
    { messageCallback_ = cb; }
    void setWriteCompleteCallback(const WriteCompleteCallback &cb)
    { writeCompleteCallback_ = cb; }
    void setCloseCallback(const CloseCallback &cb)
    { closeCallback_ = cb; }

    /*对应的还有静态成员函数和非静态成员函数*/
    static bool isET;       /*所有对象共享的*/
    static const char* srcDir;
    static std::atomic<int> userCount;


private:
    const std::string name_;
    enum StateE
    {
        kDisconnected, // 已经断开连接
        kConnecting,   // 正在连接
        kConnected,    // 已连接
        kDisconnecting // 正在断开连接
    };

    InetAddress localAddr_;
    const InetAddress peerAddr_;
    EventLoop *loop_; // 这里是baseloop还是subloop由TcpServer中创建的线程数决定 若为多Reactor 该loop_指向subloop 若为单Reactor 该loop_指向baseloop



    // 这些回调TcpServer也有 用户通过写入TcpServer注册 TcpServer再将注册的回调传递给TcpConnection TcpConnection再将回调注册到Channel中
    ConnectionCallback connectionCallback_;       // 有新连接时的回调
    MessageCallback messageCallback_;             // 有读写消息时的回调
    WriteCompleteCallback writeCompleteCallback_; // 消息发送完成以后的回调
    HighWaterMarkCallback highWaterMarkCallback_; // 高水位回调
    CloseCallback closeCallback_; // 关闭连接的回调

    // Socket Channel 这里和Acceptor类似    Acceptor => mainloop    TcpConnection => subloop
    std::unique_ptr<Socket> socket_;
    std::unique_ptr<Channel> channel_;

    bool isClose_;
    int iovCnt_;

    struct iovec iov_[2];

    /*读写缓冲区*/
    Buffer readBuff_;
    Buffer writeBuff_;

    HttpRequest request_;
    HttpResponse response_;
};

#endif